# encoding: utf-8
class UsersController < ApplicationController

  def index
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @users = User.all
      @roles = Security::UserRole.find(:all,:conditions=>{:deleted=>[0]})
      respond_to do |format|
        format.html # index.html.erb
        format.xml  { render :xml => @users }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end 

  def show
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])

      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @user }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def new
    @title_view = 'Usuarios'
    @user = User.new
    @roles = Security::UserRole.find(:all,:conditions=>{:deleted=>[0]})
    @cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:deleted=>[0]})
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @user }
    end
  end

  def edit_role_and_institution
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])
      @roles = Security::UserRole.find(:all,:conditions=>{:deleted=>[0]})
      @cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:deleted=>[0]})
      respond_to do |format|
        format.html # edit.html.erb
        format.xml  { render :xml => @user }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def update_role_and_institution
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user_aux = User.find(params[:id])
      @user = User.new(params[:user])
      @user_aux.role = @user.role
      @user_aux.cultural_institution = @user.cultural_institution
      @user_aux.name = @user.name
      @user_aux.lastname = @user.lastname
      @user_aux.save
      respond_to do |format|
        format.html { redirect_to(:controller => '/users', :action => 'index') }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  
  def aprove
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])
      @user.state=1
      @user.save
      UserMailer.approved_email(@user).deliver
      respond_to do |format|
        format.html { redirect_to(:controller => '/users', :action => 'index') }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def reject
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])
      @user.state=2
      @user.save
      UserMailer.rejected_email(@user).deliver
      respond_to do |format|
        format.html { redirect_to(:controller => '/users', :action => 'index') }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
  
  def suspend
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])
      @user.state=3
      @user.save
      respond_to do |format|
        UserMailer.suspended_email(@user).deliver
        format.html { redirect_to(:controller => '/users', :action => 'index') }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def delete
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user = User.find(params[:id])
      mensaje = @user.erasable
      if mensaje.blank?
        @user.deleted = 1
        @user.save
      end
      respond_to do |format|
        format.html { redirect_to({:controller => '/users', :action => 'index'}, :notice => mensaje) }
        format.xml  { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  def update_table
    if (user_signed_in? && ([2].include?(current_user.role)))
      @title_view = 'Usuarios'
      @user_name = params[:user_name]
      @user_role = params[:user_role]
      @user_state = params[:user_state]
      @users = User.all
      if !@user_name.blank? && @user_role.blank? && @user_state.blank?
        @users = User.find(:all,:conditions=>["users.name like ? or users.lastname like ? or users.username like ?", "%#{@user_name}%", "%#{@user_name}%", "%#{@user_name}%"])
      end

      if @user_name.blank? && !@user_role.blank? && @user_state.blank?
        @users = User.find(:all,:conditions=>["users.role = ?", @user_role])
      end

      if @user_name.blank? && @user_role.blank? && !@user_state.blank?
        @users = User.find(:all,:conditions=>["users.state = ?", @user_state])
      end

      if !@user_name.blank? && !@user_role.blank? && @user_state.blank?
        @users = User.find(:all,:conditions=>["users.role = ? and (users.name like ? or users.lastname like ? or users.username like ?)", @user_role, "%#{@user_name}%", "%#{@user_name}%", "%#{@user_name}%"])
      end

      if @user_name.blank? && !@user_role.blank? && !@user_state.blank?
        @users = User.find(:all,:conditions=>["users.role = ? and users.state=?", @user_role, @user_state])
      end

      if !@user_name.blank? && @user_role.blank? && !@user_state.blank?
        @users = User.find(:all,:conditions=>["users.state = ? and (users.name like ? or users.lastname like ? or users.username like ?)", @user_state, "%#{@user_name}%", "%#{@user_name}%", "%#{@user_name}%"])
      end

      if !@user_name.blank? && !@user_role.blank? && !@user_state.blank?
        @users = User.find(:all,:conditions=>["users.role = ? and (users.name like ? or users.lastname like ? or users.username like ?) and users.state = ?", @user_role, "%#{@user_name}%", "%#{@user_name}%", "%#{@user_name}%", @user_state])
      end

      render :partial => "table"
    
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
end
